# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate:
# Message:

import requests
import sys
from lxml import etree

# 解决编码问题
reload(sys)
sys.setdefaultencoding('utf-8')

pid = "11002357158"


url = "https://www.dwp.bmw-brilliance.cn/sap(bD16aCZjPTEwMA==)/bc/bsp/bmw/gis_tp_par_dev/spp05_stock_check.htm"
my_cookies = {
    'sap-contextid': "SID%3aANON%3apcqpdi33_CQP_33%3aal--bsHUsURzw8XTw8h3JNmIJuTnfMhzXhk3j2q--NEW",
    "sap-appcontext": "c2FwLXNlc3Npb25pZD1TSUQlM2FBTk9OJTNhcGNxcGRpMzNfQ1FQXzMzJTNhYWwtLUVCenVJZldDVnU2MW1CNkpTaEVrVUQzc2ZNaFlDaGxiV05wTy1BVFQ%3d",
    "SelectedMaterials": "",
    "Order_Selected_materials": "",
    "sap-usercontext": "sap-client=100",
    "BIGipServercqpcn.bmwbrill.cn_pool_8443": "3528863392.64288.0000",
    "MYSAPSSO2": "AjQxMDMBABg0ADAANAAyADkAUABBAFIAIAAgACAAIAACAAYxADAAMAADABBDAFEAUAAgACAAIAAgACAABAAYMgAwADEAOAAwADcAMgA2ADAAMgA1ADEABQAEAAAACAYAAlgACQACMQD%2fAVYwggFSBgkqhkiG9w0BBwKgggFDMIIBPwIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBMYIBHjCCARoCAQEwbzBkMQswCQYDVQQGEwJERTEcMBoGA1UEChMTU0FQIFRydXN0IENvbW11bml0eTETMBEGA1UECxMKU0FQIFdlYiBBUzEUMBIGA1UECxMLSTAwMjA0NzQyNjExDDAKBgNVBAMTA0NRUAIHIBUJAglTWTAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTgwNzI2MDI1MTM3WjAjBgkqhkiG9w0BCQQxFgQUAWscGja36SWqroDFcHJ%21h%2fhDeLgwCQYHKoZIzjgEAwQvMC0CFAjCrM3Sx2D9wvczUSbSqkkZ%21eRSAhUAoLDaaa4v642bJ3CUL0fcAgzPY3Y%3d",
    "lf_actionspage": "spp05_order_create.htm%3foninputprocessing(update)",
    "ordtype_create": "",
    "date_create": "",
    "po_create": "",
    "sppbasket": "",
    "sap-ssolist": "O3I9cGNxcGRpMzNfQ1FQXzMzXzEwMF81NkU5MEFFNURDN0JENkU4Qjc3NzM4NDhBM0E4NUY0NzE0RTRBRjQz",
    "SAP_SESSIONID_CQP_100": "X80qxBFSqjwRakGApOBrq2CEWgORPxHoo6wAFj4uVso%3d",
}

my_headers = {
    "Host": "www.dwp.bmw-brilliance.cn",
    "Authorization": "Basic NDA0MjlwYXI6MXEydzNlNHI=",
    "Origin": "https://www.dwp.bmw-brilliance.cn",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    "Referer": "https://www.dwp.bmw-brilliance.cn/sap(bD16aCZjPTEwMA==)/bc/bsp/bmw/gis_tp_par_dev/spp05_stock_check.htm",
}
post_data = {
    "onInputProcessing": "atp_all",
    "contentClip": "",
    "req_date": "27.07.2018",
    "material": "{}".format(pid),
    "x": "7",
    "y": "7",
}

res2 = requests.post(url=url, headers=my_headers, cookies=my_cookies, data=post_data, verify=False, timeout=30)
html2 = etree.HTML(res2.content.decode('utf8'))
# detail_url = html2.xpath('//table[@class="formul_g"]/tbody/tr[5]/td[2]/a[@class="link"]/@href')
detail_url = html2.xpath('//a[text()="11002357158"]/@href')
base_url = "https://www.dwp.bmw-brilliance.cn/sap(bD16aCZjPTEwMA==)/bc/bsp/bmw/gis_tp_par_dev/"
# print detail_url[0]

detail_url = base_url + detail_url[0]

headers = res2.headers
cookie = res2.cookies.get_dict()
for k in cookie:
    my_cookies[k] = cookie[k]

with open('./demo2.html', 'w') as f:
    f.write(res2.content.decode('utf8'))

print headers
print cookie

print detail_url
res3 = requests.get(url=detail_url, headers=my_headers, cookies=my_cookies, timeout=30)

headers = res3.headers
cookie = res3.cookies.get_dict()
for k in cookie:
    my_cookies[k] = cookie[k]

with open('./demo2(1).html', 'w') as f:
    f.write(res3.content.decode('utf8'))
print "res3", headers
print "res3", cookie

html3 = etree.HTML(res3.content.decode('utf8'))
price = html3.xpath(u'//td[contains(text(),"零售价")]/following-sibling::td[1]/text()')[0]
stock_price = html3.xpath(u'//td[contains(text(),"净值 (Stock)")]/following-sibling::td[1]/text()')[0]
rush_price = html3.xpath(u'//td[contains(text(),"净值 (Rush)")]/following-sibling::td[1]/text()')[0]

price = float(price.replace(' ', '').replace(',', '').replace('RMB', '').replace('\n', '')) * 100
stock_price = float(stock_price.replace(' ', '').replace(',', '').replace('RMB', '').replace('\n', '')) * 100
rush_price = float(rush_price.replace(' ', '').replace(',', '').replace('RMB', '').replace('\n', '')) * 100

print pid, price, stock_price, rush_price



